home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-24 | 9.0 KB | 428 lines | [TEXT/MPS ] |
- #include <stdio.h>
- #include "InputSprocket.h"
- #include "SIOUX.h"
-
- void ShowFourByte(OSType fourByte);
- void ShowFourByte(OSType fourByte)
- {
- putchar(((fourByte & 0xff000000) >> 24));
- putchar(((fourByte & 0x00ff0000) >> 16));
- putchar(((fourByte & 0x0000ff00) >> 8));
- putchar(((fourByte & 0x000000ff) >> 00));
- }
-
- void ShowStr63(const Str63 &theStr);
- void ShowStr63(const Str63 &theStr)
- {
- int i;
-
- for(i=1; i <= theStr[0]; i++)
- {
- putchar(theStr[i]);
- }
- }
-
-
- void PrintEvent(ElementEventPtr theEvent);
- void PrintEvent(ElementEventPtr theEvent)
- {
- printf("when = [%lu] [%lu]\n", theEvent->when.hi, theEvent->when.lo);
- printf("data = %ld\n", theEvent->data);
- printf("element = %lx\n", theEvent->element);
- printf("refcon = %lx\n", theEvent->refCon);
-
- ElementInfo info;
- GetElementInfo(theEvent->element, &info);
-
- printf("label = ");
- ShowFourByte(info.theLabel);
- printf("\n");
-
- printf("kind = ");
- ShowFourByte(info.theKind);
- printf("\n");
-
- printf("string = ");
- ShowStr63(info.theString);
- printf("\n");
-
- printf("\n\n");
- }
-
- OSErr TwoButtonJoystick(UInt32 &status, ElementReference &lastXAxis, ElementReference &lastYAxis,
- SInt32 &xAxis, SInt32 &yAxis, SInt32 &POVHat, Boolean &button1, Boolean &button2);
- OSErr TwoButtonJoystick(UInt32 &status, ElementReference &lastXAxis, ElementReference &lastYAxis,
- SInt32 &xAxis, SInt32 &yAxis, SInt32 &POVHat, Boolean &button1, Boolean &button2)
- {
- enum
- {
- kButton1ShouldBe0 = 0x00000001,
- kButton2ShouldBe0 = 0x00000002
- };
-
- if (status & kButton1ShouldBe0)
- {
- button1 = false;
- }
-
- if (status & kButton2ShouldBe0)
- {
- button2 = false;
- }
-
- status = 0;
-
- Boolean button1MustBeDown = false;
- Boolean button2MustBeDown = false;
-
- int maxEvents = 10; // only do events ten at a time, they might be continuously streaming out
- ElementEvent theEvent;
- Boolean wasEvent = true;
- OSErr err = noErr;
- ElementListReference globalList;
-
- err = GetGlobalElementList(&globalList);
-
- while(maxEvents > 0)
- {
- err = ElementListGetNextEvent(globalList, sizeof(theEvent), &theEvent, &wasEvent);
-
- if ((wasEvent == false) || (err))
- {
- break;
- }
-
- // get information about this element since we are not doing any configuration
- // we need to know the about labels and kinds
- ElementInfo info;
- GetElementInfo(theEvent.element, &info);
-
- switch(info.theKind)
- {
- case kElementKindButton:
- {
- ButtonConfigurationInfo buttonConfig;
- Boolean newValue;
-
- // figure out the new value for the boolean
- if (theEvent.data)
- {
- newValue = true;
- }
- else
- {
- newValue = false;
- }
-
- // need configuration information to determine if this is button1 or not
- GetElementConfigurationInfo(theEvent.element, sizeof(buttonConfig), &buttonConfig);
-
- // we will call button id 1 our buttons 1, usually the trigger
- if (buttonConfig.id == 1)
- {
- if (newValue)
- {
- button1MustBeDown = true;
- }
-
- button1 = newValue;
- }
- else
- {
- button2 = newValue;
-
- if (newValue)
- {
- button2MustBeDown = true;
- }
- }
- }
- break;
-
- case kElementKindDPad:
- {
- if (info.theLabel == kElementLabelPOVHat)
- {
- POVHat = theEvent.data;
- }
- else if (info.theLabel == kElementLabelPadMove)
- {
- // a movement DPad so they might be using
- // a console style direction pad so turn
- // that into axis style data
-
- lastXAxis = nil;
- lastYAxis = nil;
-
- switch(theEvent.data)
- {
- case kPadIdle:
- xAxis = kAxisCenter;
- yAxis = kAxisCenter;
- break;
- case kPadLeft:
- xAxis = kAxisMinimum;
- yAxis = kAxisCenter;
- break;
- case kPadUpLeft:
- xAxis = kAxisMinimum;
- yAxis = kAxisMaximum;
- break;
- case kPadUp:
- xAxis = kAxisCenter;
- yAxis = kAxisMaximum;
- break;
- case kPadUpRight:
- xAxis = kAxisMaximum;
- yAxis = kAxisMaximum;
- break;
- case kPadRight:
- xAxis = kAxisMaximum;
- yAxis = kAxisCenter;
- break;
- case kPadDownRight:
- xAxis = kAxisMaximum;
- yAxis = kAxisMinimum;
- break;
- case kPadDown:
- xAxis = kAxisCenter;
- yAxis = kAxisMinimum;
- break;
- case kPadDownLeft:
- xAxis = kAxisMinimum;
- yAxis = kAxisMinimum;
- }
- }
- }
- break;
-
- case kElementKindAxis:
- {
- // if it is an axis find out if it is the
- // x or y axis style data and use that.
-
- if (info.theLabel == kElementLabelXAxis)
- {
- lastXAxis = theEvent.element;
-
- xAxis = theEvent.data;
- }
- else if (info.theLabel == kElementLabelYAxis)
- {
- lastYAxis = theEvent.element;
-
- yAxis = theEvent.data;
- }
- }
- break;
- }
-
- maxEvents--;
- }
-
- if (button1MustBeDown && !(button1))
- {
- button1 = true;
-
- status |= kButton1ShouldBe0;
- }
-
- if (button2MustBeDown && !(button2))
- {
- button2 = true;
-
- status |= kButton2ShouldBe0;
- }
-
- // poll the last meaningful axis we had on the way out
- // just to make sure we had good data
- if (lastXAxis != nil)
- {
- GetElementSimpleState(lastXAxis, &xAxis);
- }
-
- if (lastYAxis != nil)
- {
- GetElementSimpleState(lastYAxis, &yAxis);
- }
-
- return err;
- }
-
- ElementReference gLocalButton = nil;
-
- void CreateLocalButton(void);
- void CreateLocalButton(void)
- {
- return;
-
- ButtonConfigurationInfo thisButtonConfiguration;
- thisButtonConfiguration.id = 1;
-
- ElementDefinitionStruct localButtonConfig;
-
- localButtonConfig.group = 0;
- localButtonConfig.device = nil;
- Str63 localButtonConfigString = "\pLocal Button";
- ::BlockMove(&localButtonConfigString, &localButtonConfig.theString, localButtonConfigString[0] + 1);
- localButtonConfig.configInfo = &thisButtonConfiguration;
- localButtonConfig.configInfoLength = sizeof(thisButtonConfiguration);
- localButtonConfig.kind = kElementKindButton;
- localButtonConfig.label = 'none';
- localButtonConfig.dataSize = 4;
-
- NewElement(&localButtonConfig, &gLocalButton);
- }
-
- void PushLocalButton(void);
- void PushLocalButton(void)
- {
- return;
-
- static SInt32 lastPush = 0;
- AbsoluteTime time;
-
- time.hi = 0;
- time.lo = 0;
-
- if (lastPush == 0)
- {
- lastPush = 1;
- }
- else
- {
- lastPush = 0;
- }
-
- ElementPushSimpleData(gLocalButton, lastPush, &time);
- }
-
-
- void PrintElementBlock(ElementReference *theElementReferences, UInt32 count);
- void PrintElementBlock(ElementReference *theElementReferences, UInt32 count)
- {
- printf("count = %d\n");
-
- int itr;
-
- for(itr = 0; itr < count; itr++)
- {
- printf("element #%d\n",itr);
-
- ElementInfo info;
-
- GetElementInfo(theElementReferences[itr], &info);
-
- printf("label = ");
- ShowFourByte(info.theLabel);
- printf("\n");
-
- printf("kind = ");
- ShowFourByte(info.theKind);
- printf("\n");
-
- printf("string = ");
- ShowStr63(info.theString);
- printf("\n\n");
- }
-
- printf("\n");
- }
-
- void main(void)
- {
- ElementReference lastXAxis = nil;
- ElementReference lastYAxis = nil;
- SInt32 xAxis = 0;
- SInt32 yAxis = 0;
- SInt32 POVHat = 0;
- Boolean button1 = 0;
- Boolean button2 = 0;
- UInt32 status = 0;
-
- printf("starting up...\n");
- CreateLocalButton();
-
-
- // printf("msr = %lx\n",GetMSR());
-
- ElementListReference globalList;
- OSErr err;
-
- err = GetGlobalElementList(&globalList);
- if (err)
- {
- printf("error getting global list (%ld)\n",err);
- }
-
- UInt32 bufferSize = 5;
- ElementReference theElementReferences[200];
- UInt32 count;
-
- printf("extracting the elements!\n");
- ElementListExtract(globalList, bufferSize, &count, theElementReferences);
- PrintElementBlock(theElementReferences, count);
-
- printf("kind = trigger\n");
- ElementListExtractByKind(globalList, kElementKindTrigger, bufferSize, &count, theElementReferences);
- PrintElementBlock(theElementReferences, count);
-
- printf("kind = button\n");
- ElementListExtractByKind(globalList, kElementKindButton, bufferSize, &count, theElementReferences);
- PrintElementBlock(theElementReferences, count);
-
- printf("kind = direction pad\n");
- ElementListExtractByKind(globalList, kElementKindDPad, bufferSize, &count, theElementReferences);
- PrintElementBlock(theElementReferences, count);
-
- printf("kind = axis\n");
- ElementListExtractByKind(globalList, kElementKindAxis, bufferSize, &count, theElementReferences);
- PrintElementBlock(theElementReferences, count);
-
- printf("label = xaxis\n");
- ElementListExtractByLabel(globalList, kElementLabelXAxis, bufferSize, &count, theElementReferences);
- PrintElementBlock(theElementReferences, count);
-
- while(1)
- {
- #if 1
- ElementEvent event;
- Boolean wasEvent;
-
- ElementListGetNextEvent(globalList, sizeof(event), &event, &wasEvent);
-
- if (wasEvent)
- {
- PrintEvent(&event);
- }
-
- #else
-
- PushLocalButton();
- TwoButtonJoystick(status, lastXAxis, lastYAxis, xAxis, yAxis, POVHat, button1, button2);
- printf("xAxis = %ld\n",xAxis);
- printf("yAxis = %ld\n",yAxis);
- printf("POVHat = %ld\n",POVHat);
- printf("Button 1 = %d\n",button1);
- printf("Button 2 = %d\n",button2);
-
- printf("return\n");
- char foo[255];
- scanf("%s\n",foo);
-
- #endif
- KeyMap theKeys;
- GetKeys(theKeys);
-
- if ((theKeys[1] & 0x8000) && (theKeys[1] & 0x800000))
- {
- break;
- }
-
- SIOUXHandleOneEvent(nil);
- }
-
-
- printf("later\n");
- }
-